package com.tianqi.ui.dao;

import com.tianqi.ui.bean.WeatherInfo;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class WeatherInfoDAO extends BaseDAO{
    private Connection con = null;
    private Statement st = null;
    private ResultSet rs = null;

    public List<WeatherInfo> queryByDataCity(String city){
        List<WeatherInfo> ws = new ArrayList<>();
        String sql = "select * from w_info where city ='"+city+"'";
        try {
            con = getCon();
            st = con.createStatement();
            rs = st.executeQuery(sql);
            while (rs.next()){
                String w_time = rs.getString("w_time");
                String high = rs.getString("high");
                String fengli = rs.getString("fengli");
                String low = rs.getString("low");
                String fengxiang = rs.getString("fengxiang");
                String w_type = rs.getString("w_type");
                String w_city = rs.getString("city");

                WeatherInfo w = new WeatherInfo();
                w.setDate(w_time);
                w.setHigh(high);
                w.setFengli(fengli);
                w.setLow(low);
                w.setFengxiang(fengxiang);
                w.setType(w_type);
                w.setCity(w_city);
                // 讲创建好的对象加入集合
                ws.add(w);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(rs,st,con);
        }
        return ws;
    }
    public int save(WeatherInfo w){
        String sql = "insert into w_info values('"+w.getDate()+"','"+w.getHigh()+"','"+w.getFengli()+"','"+w.getLow()+"','"+w.getFengxiang()+"','"+w.getType()+"','"+w.getCity()+"')";
        try {
            con = getCon();
            st = con.createStatement();
            return st.executeUpdate(sql);
        } catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(rs,st,con);
        }
        return 0;
    }
}
